<template>
  <div>
    <div class="container" style="border: none;">
      <el-row style="margin-bottom: 10px;">
        <el-col :span="3">
          <el-button type="success" icon="el-icon-circle-plus" @click="handleEidt(0)">添加文章</el-button>
        </el-col>
        <el-col :span="3">
          <el-input
            placeholder="名称"
            v-model="title"
            clearable>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-input
            placeholder="简介"
            v-model="resume"
            clearable>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-select v-model="is_available" placeholder="审核" clearable>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-date-picker
            v-model="create_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="创建日期">
          </el-date-picker>
        </el-col>
        <el-col :span="6">
          <el-cascader placeholder="栏目" class="my-container" clearable v-model="menu_id" :options="articleClass" :props="{value: 'id', label: 'label', checkStrictly: true }"></el-cascader>
        </el-col>
        <el-col :span="2">
          <el-button type="success" @click="search()">搜索</el-button>
        </el-col>
      </el-row>
      <el-table :data="list" border v-loading="ifload">
        <el-table-column prop="title" label="标题" align="center" width="300">
        </el-table-column>
        <el-table-column prop="resume" align="center" label="描述" width="300">
        </el-table-column>
        <el-table-column prop="image" align="center" label="Logo图">
          <template slot-scope="scope">
            <img :src="scope.row.image" width="30px" />
          </template>
        </el-table-column>
        <el-table-column v-if="roleId == 1" prop="is_available" align="center" label="审核" width="120">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.is_available" @change="availableChange(scope.row.id,scope.row.is_available)" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
            <span>{{scope.row.is_available?'已审核':'未审核'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-document" @click="handleEidt(scope.row.id)">
              修改
            </el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDel(scope.$index, scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="pages" :current-page="page" :page-size="page_size">
        </el-pagination>
      </div>

    </div>

    <!-- 删除提示框 -->
    <el-dialog title="删除提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button :loading="ifload" type="primary" @click="delData">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script type="text/javascript">
import { download } from '@/components/js/request'
import upload from '@/components/utils/upload';
export default {
  components: { upload },
  data() {
    return {
      roleId: 0,
      ifload: true,
      page: 1,
      page_size: 10,
      pages: 0,
      list: [],
      //搜索用
      is_available:'',
      options: [{
          value: true,
          label: '已审核'
        }, {
          value: false,
          label: '未审核'
      }],
      title: '',
      resume: '',
      menu_id: '',
      create_time: '',
      //当前操作对象
      curId: 0,
      curIndex: -1,
      delVisible: false,
      editVisible: false,
      articleClass: [],

    }
  },
  created() {
    this.$post_('admin/user/user_info', {}, (res) => {
      this.roleId = res.data.role_id;
    });
    
    if(this.$route.query.page){
      this.page = this.$route.query.page
    }
    this.$nextTick(() => {
      this.getData();
    })
  },

  methods: {
    getData() {
      this.ifload = true;
      this.$post_('setting/menu/list', {}, (res) => {
        this.articleClass = res.data;
      })
      this.$post_('content/content/list', 
      { 
        page: this.page, 
        title: this.title,
        resume: this.resume,
        is_available: this.is_available == true ? 1 : this.is_available === false ? 0 : '',
        menu_id: this.menu_id,
        create_time: this.create_time
      }, (res) => {
        if (res.code == '0') {
          this.list = res.data.map(item => {
            item.is_available = item.is_available == 1 ? true : false
            return item
          });;
          this.pages = Number(res.extend.pages);
          this.ifload = false;
        }
      });
    },
    search(){
      console.log(this.create_time)
      this.ifload = true;
      //选二级的时候会返回[15,38]这样的数据
      if(this.menu_id){
        if (this.menu_id.length>1){
          this.menu_id = this.menu_id[this.menu_id.length-1];
        } else {
          this.menu_id = this.menu_id[0];
        }
      }
      console.log(this.is_available)
      this.$post_('content/content/list', 
      { 
        page: this.page, 
        title: this.title,
        resume: this.resume,
        is_available: this.is_available == true ? 1 : this.is_available === false ? 0 : '',
        menu_id: this.menu_id,
        create_time: this.create_time
      }, (res) => {
        if (res.code == '0') {
          this.list = res.data.map(item => {
            item.is_available = item.is_available == 1 ? true : false
            return item
          });;
          this.pages = Number(res.extend.pages);
          this.ifload = false;
        }
      });
    },
    // 分页导航
    handleCurrentChange(val) {
      
      this.page = val;
      this.getData();
      let query = this.$router.history.current.query;
      let path = this.$router.history.current.path;
      //对象的拷贝
      let newQuery = JSON.parse(JSON.stringify(query));
      // 地址栏的参数值赋值
      newQuery.page = val;
      this.$router.push({ path, query: newQuery });
    },

    //修改
    handleEidt(id) {
      this.$router.push({ path: '/page/platform/article_edit', query: { id: id } })
    },

    availableChange(id, value) {
      this.$post_('content/content/update', { id: id, is_available: (value ? 1 : 0) }, (res) => {
        if (res.code == '0') {
          this.$message.success(res.msg);
        }
      });
    },
    //删除确认
    handleDel(index, row) {
      this.delVisible = true;
      this.curId = row.id;
      this.curIndex = index;
    },
    showImg(url) {
      this.form.img_url = url;
    },
    //删除
    delData() {
      this.ifload = true;
      let param = { id: this.curId };
      this.$post_('content/content/delete', param, (res) => {
        console.log(res);
        if (res.code == '0') {
          this.$message.success(res.msg);
          this.ifload = false;
          this.delVisible = false;
          this.getData();
        } else {
          this.$message.error(res.msg);
        }
      })
    },
  }
}
</script>

<style lang="css">
  .el-popper .el-radio{
    width: 100%;
    height: 100%;
    z-index: 10;
    position: absolute;
    display: flex;
    align-items: center;
  }
  .el-popper .el-radio .el-radio__input{
    top:1px;
  }
  .el-popper .el-radio+.el-cascader-node__label{
    margin-left: 10px;
  }
  .el-popper .edui-default .edui-toolbar{
    line-height: normal !important;
  }
  .el-date-editor.el-input{
    width:100% !important;
  }
  thead tr th {
    text-align: center;
  }
  .red {
    color: #ff0000;
  }
  .search {
    margin-bottom: 10px;
  }
</style>
